package cn.ren.controller;

import cn.ren.constant.MessageConstant;
import cn.ren.entity.PageResult;
import cn.ren.entity.QueryPageBean;
import cn.ren.entity.Result;
import cn.ren.pojo.LZOrderShow;
import cn.ren.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;
import java.util.Map;

/**
 * @program: itcast_health
 * @description:
 * @author: ren
 * @create: 2020-03-04 21:37
 **/
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    public String getUsernameBase(){
        //从spring-securitty保存在session中的user拿出来,通过其上下文对线的方法获得。
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return user.getUsername();
    }

    @RequestMapping("/getUsername")
    public Result getUsername(){

        String username;
        try {
            username = getUsernameBase();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    @RequestMapping("/getMenu")
    public Result getMenu(){
        //获取用户名
        String username = getUsernameBase();
        try {
            List<Map<String,Object>> list = userService.findMenuByUsername(username);
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }

    @RequestMapping("/lzFindPage")
    public PageResult lzFindPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userService.lzFindPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/lzAdd")
    public Result lzAdd(@RequestBody cn.ren.pojo.User user, int[] checkgroupIds){
        return  userService.lzAdd(user,checkgroupIds);
    }
}
